﻿using com.nm.library.Excels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.library;
using System.Data;
using com.nm;
using com.nm.db.Models.Systems;
using System.ComponentModel.Composition;
using george.Helper;

namespace OrderAPI.ImportExcel
{
    [Export(typeof(ExcelImport))]
    public class OrderSKCRequireExcel : ExcelImport
    {
        /// <summary>
        /// Excel字段映射及校验缓存
        /// </summary>
        /// <returns>字段映射</returns>
        public override Dictionary<string, ImportVerify> DictFields => dictFields;

        private static Dictionary<string, ImportVerify> dictFields = new List<ImportVerify> {
            new ImportVerify{ ColumnName="系列",FieldName="SerialName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},
            new ImportVerify{ ColumnName="商品层",FieldName="MaterialProperty",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},
            new ImportVerify{ ColumnName="项目",FieldName="ItemName",DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true) },
            new ImportVerify{ ColumnName="需求SKC数",FieldName="SKCQty",DataType=typeof(int),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,4,true,true)}
        }.ToDictionary(e => e.ColumnName, e => e);

        public override ExcelImportType Type => ExcelImportType.ORDERSKCREQUIRE;

        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int MasterID)
        {
            var index = dt.Rows.Count;
            var msg = $"成功导入{index}条数据";
            try
            {
                OrderAPI.OrderMasterHandler omh = new OrderMasterHandler();
                omh.ImportOrderSKCRequire(dt, MasterID);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return new ImportResult()
                {
                    IsSuccess = false,
                    ExtraInfo = ex,
                    Message = ex.Message
                };
            }
            return new ImportResult()
            {
                IsSuccess = true,
                ExtraInfo = "导入成功",
                Message = msg
            };
        }

        public override DataTable GetExportData(string Keys)
        {
            throw new NotImplementedException();
        }

        public static void WriteLog(int userId, int type, string code, string context, string memo)
        {
            ////var Id =  UserInfo.UserID==""?0:Convert.ToInt16(UserInfo.UserID);
            //var dc = new TerminalEntities();
            //var log = new Sys_ChangeLog
            //{
            //    Context = context,
            //    Memo = memo,
            //    Operator = userId,
            //    OperatorDate = DateTime.Now,
            //    Code = code,
            //    Type = type
            //};
            //dc.Sys_ChangeLog.Add(log);
            //try
            //{
            //    dc.SaveChanges();
            //}
            //catch
            //{
            //    throw;
            //}
        }
    }
}
